<template>
  <div
    :style="{
      overflow: 'hidden',
      position: 'relative',
      height: '100%',
    }"
  >
    <!-- 注册table -->
    <BasicTable @register="registerTable">
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              tooltip: '查看',
              icon: 'clarity:info-standard-line',
              onClick: handleView.bind(null, { data: record }),
            },
            {
              tooltip: '编辑',
              icon: 'clarity:note-edit-line',
              onClick: handleEdit.bind(null, { data: record }),
            },
            {
              tooltip: '删除',
              color: 'error',
              icon: 'ant-design:delete-outlined',
              popConfirm: {
                title: '是否确定删除？',
                confirm: handleDel.bind(null, record),
              },
            },
          ]"
        />
      </template>
      <template #toolbar>
        <a-button type="primary">{{ '新增' }}</a-button>
      </template>
    </BasicTable>

    <ViewDrawer @reload="handleReload" @register="registerDrawer" />
    <!-- 页面引入 -->
    <EditModal @reload="handleReload" @register="registerModal" />
  </div>
</template>

<script lang="ts">
  import { defineComponent } from 'vue';
  import { formConfig, columns, initData } from './data';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { useMessage } from '/@/hooks/web/useMessage';
  import ViewDrawer from './ViewDrawer.vue';
  import EditModal from './EditModal.vue';
  export default defineComponent({
    name: 'tableTest',
    components: {
      BasicTable,
      TableAction,
      ViewDrawer,
      EditModal
    },
    setup() {
      const data = initData();

      const { createMessage } = useMessage();
      // 设置table
      const [registerTable, { reload }] = useTable({
        title: '查询结果',
        dataSource: data,
        columns: columns,
        bordered: true,
        useSearchForm: true, //开启搜索区域
        formConfig: formConfig,
        actionColumn: {
          width: 120,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
        },
        rowSelection: { type: 'radio' },
        pagination: { pageSize: 10 },
        showTableSetting: true,
        tableSetting: { fullScreen: true },
        showIndexColumn: true,
        indexColumnProps: { fixed: 'left' },
      });

      // 注册Drawer
      const [registerDrawer, { openDrawer }] = useDrawer();

      // 配置Modal
      const [registerModal, { openModal }] = useModal();

      // 查看按钮
      function handleView({ data }) {
        // 打开弹窗，传data
        openDrawer(true, data);
      }

      // 编辑按钮
      function handleEdit({ data }) {
        openModal(true, data);
      }

      // 删除按钮
      function handleDel(record){
        console.log('删除数据',record);
        createMessage.success('删除成功');
        reload()
      }

      function handleReload() {
        reload();
      }
      return {
        registerTable,
        reload,

        // 查看抽屉
        registerDrawer,
        handleView,
        handleReload,

        // 编辑弹窗
        registerModal,
        handleEdit,

        // 删除
        handleDel
      };
    },
  });
</script>

<style scoped></style>
